# Test remote API connectivity
try {
    Write-Host "Testing remote server connectivity..."
    
    # Test basic connectivity
    $pingResult = Test-NetConnection -ComputerName "10.1.200.169" -Port 6724 -InformationLevel Quiet
    if ($pingResult) {
        Write-Host "Port 6724 is reachable" -ForegroundColor Green
    } else {
        Write-Host "Port 6724 is not reachable" -ForegroundColor Red
        exit 1
    }
    
    # Test login API
    $loginData = @{
        Username = "admin"
        Password = "Admin@666"
    }
    
    $jsonBody = $loginData | ConvertTo-Json
    Write-Host "Sending login request to: http://10.1.200.169:6724/api/Auth/login"
    
    $response = Invoke-RestMethod -Uri "http://10.1.200.169:6724/api/Auth/login" -Method Post -Body $jsonBody -ContentType "application/json" -TimeoutSec 10
    
    Write-Host "Login successful!" -ForegroundColor Green
    Write-Host "Token: $($response.token.Substring(0,20))..."
    Write-Host "User: $($response.username)"
    Write-Host "Role: $($response.role)"
    
} catch {
    Write-Host "Connection failed: $($_.Exception.Message)" -ForegroundColor Red
    
    if ($_.Exception.Response) {
        $statusCode = $_.Exception.Response.StatusCode.value__
        Write-Host "HTTP Status Code: $statusCode"
        
        if ($statusCode -eq 401) {
            Write-Host "Authentication failed"
        } elseif ($statusCode -eq 404) {
            Write-Host "API endpoint not found"
        } elseif ($statusCode -eq 500) {
            Write-Host "Internal server error"
        }
    }
    
    exit 1
}